package middleware

import (
	"github.com/gin-gonic/gin"
	"go_auth/ext/app"
	"go_auth/ext/e"
	"go_auth/ext/settings"
	"go_auth/servers/users"
	"go_auth/utils/commons"
	"net/http"
)

func CheckPageNum() gin.HandlerFunc {
	return func(c *gin.Context) {
		appG := app.Gin{C: c}

		pageNum := c.Query("page_num")
		token := c.Query("token")

		uri := c.Request.URL.Path
		method := c.Request.Method
		if commons.StringFilter(pageNum, 1) {
			if i, b := commons.StrToInt(pageNum); i > int(settings.AppBase.AllowSize) {
				appG.CheckResponse(http.StatusOK, e.UserNotLogin, nil)
			} else if !b {
				appG.CheckResponse(http.StatusOK, e.AtoB, nil)
			}
		}

		filter := commons.StringIsEmpty(token, 32)

		if filter {
			code := users.CheckToken(token, uri, method)
			if code != e.Success {
				appG.CheckResponse(http.StatusOK, code, nil)
			}
		}

		c.Next()
	}

}
